//
//  DSXAppDelegate.m
//  DerbyOSSampler
//
//  Created by glenn andreas on 1/6/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DSXAppDelegate.h"
#import "zmq.h"
#import "zmq_utils.h"
#import "DerbyNSOde.h"
#import "DerbyNSOdeLog.h"
#import "DSXScoreboardViewController.h"

void *zmqctx;
@implementation DSXAppDelegate

- (void) testHelloWorldServer
{
    NSOperationQueue *opq = [NSOperationQueue mainQueue];
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        //  Socket to talk to clients
        printf("Starting zmq hello world server");
        void *context = zmq_ctx_new ();
        void *responder = zmq_socket (context, ZMQ_REP);
        int rc = zmq_bind (responder, "tcp://*:5555");
        assert (rc == 0);
        
        while (1) {
            char buffer [10];
            zmq_recv (responder, buffer, 10, 0);
            printf ("Received Hello\n");
            //            sleep (1);          //  Do some 'work'
            zmq_send (responder, "World", 5, 0);
        }
    }];
    [opq addOperation:op];
}

- (void) testHelloWorldClient
{
    NSOperationQueue *opq = [NSOperationQueue mainQueue];
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        printf ("Connecting to hello world server…\n");
        void *context = zmq_ctx_new ();
        void *requester = zmq_socket (context, ZMQ_REQ);
        zmq_connect (requester, "tcp://localhost:5555");
        
        int request_nbr;
        for (request_nbr = 0; request_nbr != 10; request_nbr++) {
            char buffer [10];
            printf ("Sending Hello %d…\n", request_nbr);
            zmq_send (requester, "Hello", 5, 0);
            zmq_recv (requester, buffer, 10, 0);
            printf ("Received World %d\n", request_nbr);
        }
        zmq_close (requester);
        zmq_ctx_destroy (context);
    }];
    [opq addOperation:op];
}

static DerbyNSOde *gNSOde;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"DerbyNSOdeLog":@{
                                                                      @"PEER":@(DerbyNSOdeLogInfo),
                                                                      @"DNS":@(DerbyNSOdeLogInfo),
                                                                      }}];
    // Insert code here to initialize your application
//    [self testHelloWorldServer];
//    [self testHelloWorldClient];
    gNSOde = [[DerbyNSOde alloc] initWithContext:nil];
    [gNSOde pollInBackground];
    
    self.scoreboardController.nsode = gNSOde;
    for (NSViewController *vc in @[self.scoreboardController]) {
        NSTabViewItem *item = [[NSTabViewItem alloc] initWithIdentifier:vc.title];
        item.label = vc.title;
        item.view = vc.view;
        [self.tabView addTabViewItem:item];
    }
    [self.window.contentView addSubview:self.scoreboardController.view];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
    zmq_ctx_shutdown(zmqctx);
}
@end
